package com.example.wanglong.myoneapplication.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.wanglong.myoneapplication.adapter.VisaDetailAdapter;
import com.example.wanglong.myoneapplication.common.BaseActivity;
import com.example.wanglong.myoneapplication.R;
import com.example.wanglong.myoneapplication.entity.VisaDetailEntity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wang.long on 2016/4/6.
 */
public class ListActivity extends BaseActivity {

    private List<VisaDetailEntity> visaList=new ArrayList<VisaDetailEntity>();
    private String city;
    private LinearLayout v;
    private TextView listtitle;
    private LinearLayout listback;
    private LinearLayout listhome;
    private RelativeLayout footvisatype;
    private RelativeLayout footlivecity;
    private RelativeLayout footsuppliers;
    private RelativeLayout footlistsort;
    private int pagesum=0;
    private ListView listView;
    private VisaDetailAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.activity_list);

        //参数接收
        Intent intentdata=getIntent();
        city=intentdata.getStringExtra("city");


        //初始化
        initData();


    }
    public void initData(){
        //元素定位
        listtitle=(TextView) findViewById(R.id.listtitle);
        listback=(LinearLayout) findViewById(R.id.listback);
        listhome=(LinearLayout) findViewById(R.id.listhome);
        footvisatype=(RelativeLayout) findViewById(R.id.footvisatype);
        footlivecity=(RelativeLayout) findViewById(R.id.footlivecity);
        footsuppliers=(RelativeLayout) findViewById(R.id.footsuppliers);
        footlistsort=(RelativeLayout) findViewById(R.id.footlistsort);

        v =(LinearLayout) findViewById(R.id.visatitle);
        v.getBackground().setAlpha(255);

        initVisa();


        //绑定事件
        listback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ActivityCollector.finishAll();
                finish();
            }
        });

        listhome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionStart(ListActivity.this);
            }
        });

        footvisatype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(ListActivity.this, ListActivity.class));
                //actionStart(ListActivity.this);
                Intent intent=new Intent(ListActivity.this,VisaType.class);
                startActivity(intent);
            }
        });


        footlivecity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionStartTwo(ListActivity.this);
            }
        });

        footsuppliers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionStartThree(ListActivity.this);
            }
        });

        footlistsort.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionStartFour(ListActivity.this);
            }
        });

    }


    public void initVisa(){

        listtitle.setText(city);

        for(int i=0;i<20;i++){
            String cdesc=city+String.valueOf(i)+"个人旅游签证";
            String tags="代送签,免面试"+","+String.valueOf(i)+"人陪同";
            String content="提前"+String.valueOf(i)+"个工作日预订";
            String thesub="携程国旅"+String.valueOf(i)+"号导游";
            int price=(i*200);
            VisaDetailEntity visa=new VisaDetailEntity(i,cdesc,tags,content,thesub,price);
            visaList.add(visa);
            pagesum=i;
        }


        adapter=new VisaDetailAdapter(ListActivity.this,R.layout.list_item,visaList);
        listView=(ListView) findViewById(R.id.list_view);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent=new Intent(ListActivity.this,BookingActivity.class);
//                startActivity(intent);
                actionStartDetail(ListActivity.this, "美国");
            }
        });

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    // 判断是否滚动到底部
                    if (view.getLastVisiblePosition() == view.getCount() - 1) {

                        for (int i = pagesum; i < (pagesum+10); i++) {
                            String cdesc = city + String.valueOf(i) + "个人旅游签证";
                            String tags = "代送签,免面试" + "," + String.valueOf(i) + "人陪同";
                            String content = "提前" + String.valueOf(i) + "个工作日预订";
                            String thesub = "携程国旅" + String.valueOf(i) + "号导游";
                            int price = (i * 200);
                            VisaDetailEntity visa = new VisaDetailEntity(i, cdesc, tags, content, thesub, price);
                            visaList.add(visa);
                            if(i==(pagesum+10))
                            {
                                pagesum=i;
                            }
                        }

                        //adapter=new VisaDetailAdapter(ListActivity.this,R.layout.list_item,visaList);
                        //adapter.add(visaList);
                        //listView.setAdapter(adapter);
                        //自定义的适配器不需要更新，直接更新arraylist visaList
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

    }

    public static void actionStart(Context context){
        Intent intent=new Intent(context,FragmentMainActivity.class);
        context.startActivity(intent);
    }

    public static void actionStartTwo(Context context){
        Intent intent=new Intent(context,LiveCity.class);
        context.startActivity(intent);
    }

    public static void actionStartThree(Context context){
        Intent intent=new Intent(context,Suppliers.class);
        context.startActivity(intent);
    }

    public static void actionStartFour(Context context){
        Intent intent=new Intent(context,ListSort.class);
        context.startActivity(intent);
    }

    public static void actionStartDetail(Context context,String city){
        Intent intent=new Intent(context,BookingActivity.class);
        context.startActivity(intent);
    }




}
